package com.ruoyi.delivery.service.impl;

import com.ruoyi.delivery.domain.DelOutBoundOrder;
import com.ruoyi.delivery.domain.DelOutBoundDetail;
import com.ruoyi.delivery.mapper.DelOutBoundOrderMapper;
import com.ruoyi.delivery.mapper.DelOutBoundDetailMapper;
import com.ruoyi.delivery.service.IDelOutBoundOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 出库订单 服务层实现
 *
 * @author hu
 */
@Service
public class DelOutBoundOrderServiceImpl implements IDelOutBoundOrderService {

    @Autowired
    private DelOutBoundOrderMapper outBoundOrderMapper;
    @Autowired
    private DelOutBoundDetailMapper outBoundDetailMapper;

    public int countOperatorTransportOrders() {
        return outBoundOrderMapper.countOperatorTransportOrders();
    }
    @Override
    public String generateTrackingNumber() {
        int count = countOperatorTransportOrders();
        String trackingNumber;
        trackingNumber = "T" + String.format("%012d", count + 1);
        return trackingNumber;
    }

    @Override
    public List<DelOutBoundOrder> filterOutBoundOrders(
            String outId,
            String warehouseId,
            String destination,
            String outcomeDateStart,
            String outcomeDateEnd,
            String expectedDeliveryTimeStart,
            String expectedDeliveryTimeEnd,
            String state,
            String evidenceRecordId,
            int page,
            int pageSize
    ) {
        // 计算分页的起始偏移量
        int offset = (page - 1) * pageSize;
        return outBoundOrderMapper.filterOutBoundOrders(
                outId,
                warehouseId,
                destination,
                outcomeDateStart,
                outcomeDateEnd,
                expectedDeliveryTimeStart,
                expectedDeliveryTimeEnd,
                state,
                evidenceRecordId,
                offset,
                pageSize
        );
    }

    @Override
    public boolean insertOutBoundOrder(DelOutBoundOrder order) {
        return outBoundOrderMapper.insertOutBoundOrder(order) > 0;
    }

    @Override
    public boolean deleteOutBoundOrder(String outId) {
        return outBoundOrderMapper.deleteOutBoundOrder(outId) > 0;
    }

    @Override
    public boolean updateOutBoundOrder(DelOutBoundOrder order) {
        return outBoundOrderMapper.updateOutBoundOrder(order) > 0;
    }

    @Override
    public String getWarehouseIdByEvidenceRecordId(String evidenceRecordId) {
        return outBoundOrderMapper.getWarehouseIdByEvidenceRecordId(evidenceRecordId);
    }

    @Override
    public List<DelOutBoundDetail> getDetailsByExitRecordId(String outId) {
        return outBoundDetailMapper.getDetailsByExitRecordId(outId);
    }
}
